import sys
global lst,itemlst,budget,cart
lst={'root':'123456'}
itemlst={'F00001':{'name':'Apple','price':'1.2'},'F00002':{'name':'Banana','price':'5.5'}}
cart={}
budget=float(0)
flag=0
def login_required(func):
    def inner(*args,**kwargs):
        if flag==0:
            print("Not Logged In")
            menu()
    return inner
def login ():
    print("Login".center(60,'-'))
    while True:
        usrname = (input("pls input user name: "))
        if usrname == 'exit':
            exit()
        pwd = (input("pls input password: "))
        if pwd=='exit':
            exit()
        if pwd == lst.get(usrname):
            print("login succeeded")
            global flag,username
            flag=1
            username=usrname
            break
        else:
            print("login failed")
def register():
    print("Register".center(60,'-'))
    while True:
        username=input("pls input user name:")
        if username=='exit':
            exit()
        if username in lst.keys() or username.isspace():
            print("illegal usrename")
        else:
            break
    while True:
        password=input("pls input password:")
        if username=='exit':
            exit()
        if password.isspace():
            print("illegal password")
        else:
            break
    newuser={username:password}
    lst.update(newuser)
    print("register succeeded")
def show():
    print("ITEMLIST".center(60,'-'))
    print(itemlst)
def exit():
    receipt()
    print("EXIT".center(60,'*'))
    sys.exit(1)
@login_required
def buy():
    print("SHOPING".center(60,"-"))
    print(itemlst)
    global budget
    while True:
        citems=input("pls input the code of goood:")
        if citems=='eixt':
            exit()
        if citems in itemlst.keys():
            break
        else:
            print("code error")
    price=itemlst.get(citems).get('price')
    name=itemlst.get(citems).get('name')
    while True:
        qty=input("pls input the quantity of goods (press 'Q'or'q' quit shopping interface) :")
        if qty=='Q' or qty=='q':
            break
        if qty=='exit':
            exit()
        if qty.isdecimal():
            if budget<float(qty)*float(price):
                print("budget is not enough")
            else:
                budget-=float(qty)*float(price)
                total=float(qty)*float(price)
                qty=int(qty)
                if name in cart.keys():
                    tcart={name:{'price':price,'qty':qty+int(cart.get(name).get('qty')),'total':total+cart.get(name).get('total')}}
                else:
                    tcart = {name: {'price': price,'qty': qty,'total': total}}
                cart.update(tcart)
                break
        else:
            print("quantity of goods error")
@login_required
def receipt():
    print("RECEIPT".center(60,"*"))
    good = list(cart)
    total = 0
    if len(good)==0:
        print("EXIT".center(60,'*'))
        sys.exit(1)
    for i in range(len(good)):
        print(good[i].center(15),end="")
        print(cart.get(good[i]).get('price').center(15),end="")
        print("{0}".format(cart.get(good[i]).get('qty')).center(15),end="")
        print("{0}".format(cart.get(good[i]).get('total')).center(15))
        total += cart.get(good[i]).get('total')
    print("total  : {0}".format(total).center(60,"*"))
    print("budget : {0}".format(budget).center(60,"*"))
@login_required
def changebudget():
    print("CHANGE BUDGET".center(60,"-"))
    global budget
    while True:
        budget=input("pls input your budget:")
        if budget=='exit':
            exit()
        if budget.isdigit() and float(budget)>0:
            budget=float(budget)
            break
        else :
            print("input error")
@login_required
def remove():
    while True:
        global budget
        print(cart)
        ritems = input("pls input good name to revise the quantity(press 'q' or 'Q' quit):")
        if ritems == 'q' or ritems == "Q":
            break
        if ritems == 'exit':
            exit()
        if ritems in cart:
            flagrqty = True
            while flagrqty:
                rqty = input("pls input number you wanna minus:")
                if int(rqty) <= cart.get(ritems).get('qty') and rqty.isdecimal():
                    if int(rqty) == cart.get(ritems).get('qty'):
                        budget += float(cart.get(ritems).get('total'))
                        cart.pop(ritems)
                    else:
                        budget += float(cart.get(ritems).get('price')) * float(rqty)
                        nqty = int(cart.get(ritems).get('qty')) - int(rqty)
                        ntotal = float(cart.get(ritems).get('total')) - float(cart.get(ritems).get('price')) * float(
                            rqty)
                        cart.update({ritems: {'price': cart.get(ritems).get('price'),'qty': nqty,'total': ntotal}})
                    flagrqty = False
                else:
                    print("input error")
            break
        else:
            print("input error")
@login_required
def check():
    print("MY CART".center(60,'-'))
    print(cart)
    print("BUDGET".center(60,'-'))
    print("{0}".format(budget).center(60))
def menu():
    print("1:show goods 2:shopping 3:change your budget: 4:remove goods 5.sign up 6.register 7.check cart")
    while True:
        print("MENU".center(60,"*"))
        action=input("pls input :")
        if action == 'exit':
            exit()
        elif action=='1':
                show()
        elif action=='2':
                buy()
        elif action=='3':
                changebudget()
        elif action=='4':
                remove()
        elif action=='5':
                login()
        elif action=='6':
                register()
        elif action=='7':
                check()

menu()